import { _decorator, Component, instantiate, Label, Node, tween, v3 } from 'cc';
import { AdComponent } from '../AdComponent';
import { EVENT_TYPE, MISSION_TYPE, REWARD_TYPE } from '../../Cube/Levels';
import { DailyShareItem } from './DailyShareItem';
import DatasSpace from '../Tool/DatasSpace';
import EventMgr from '../Tool/EventMgr';
import SkdComm from '../SkdComm';
import { showTips } from '../Tips/Tips';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('PropShare')
export class PropShare extends Component {

  @property(Node)
  viewNode: Node = null;

  @property(Node)
  listContent: Node = null;

  @property(Label)
  todayShareCountLbl: Label = null
  start() {
    EventMgr.on(EVENT_TYPE.EVENT_UPD_SHARE, this.updShareCount, this);
    AdComponent.Instance.showInterstitialFullShow();
    this.initList();
    this.updShareCount();
  }
  protected onDisable(): void {
    SkdComm.Instance.createGameClub();
  }
  protected onEnable(): void {
    SkdComm.Instance.hideGameClub();
  }
  protected onDestroy(): void {
    EventMgr.off(EVENT_TYPE.EVENT_UPD_SHARE, this.updShareCount, this);
  }

  update(deltaTime: number) {

  }

  updShareCount() {
    const count = DatasSpace.Instance.getInt('DailyMission_' + MISSION_TYPE.SHARE, 0);
    this.todayShareCountLbl.string = `${count}次`;
  }

  btnCloseSetting() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    // AdComponent.Instance.instance.playAudio(2);
    // AdComponent.Instance.hide5X5customAd();
    tween(this.viewNode).to(0.5, { position: v3(0, this.viewNode.getPosition().y + 1600, 0) }, { easing: 'backInOut' }).call(() => {
      this.node.removeFromParent();
      this.node.destroy();

    }).start();
  }

  initList() {
    let content = this.listContent;
    for (let i = 0; i < DailyShare.length; i++) {
      let item = content.children[i];;
      if (!item) {
        item = instantiate(content.children[0]);
        item.setParent(content);
      }

      item.getComponent(DailyShareItem).init(i, DailyShare[i]);
    }
  }

  private btnAllGet() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);
    for (let i = 0; i < this.listContent.children.length; i++) {
      let itemTS = this.listContent.children[i].getComponent(DailyShareItem);
      itemTS.getReward();
      showTips('领取成功');
    }
  }

  private btnShare() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    SkdComm.Instance.shareTo(
      () => {
        EventMgr.emit(EVENT_TYPE.EVENT_UPD_SHARE);
      }
    );
  }
}

const DailyShare = [
  {
    type: 0,
    shareCount: 1,
    rewardList: [
      [REWARD_TYPE.CHEHUI, 1],
      [REWARD_TYPE.COIN, 50]
    ]
  }, {
    type: 1,
    shareCount: 3,
    rewardList: [
      [REWARD_TYPE.XIAOCHU, 1],
      [REWARD_TYPE.COIN, 20]
    ]
  }, {
    type: 1,
    shareCount: 5,
    rewardList: [
      [REWARD_TYPE.FANPAI, 1],
      [REWARD_TYPE.COIN, 20]
    ]
  }, {
    type: 1,
    shareCount: 10,
    rewardList: [
      [REWARD_TYPE.XIPAI, 1],
      [REWARD_TYPE.COIN, 30]
    ]
  }
]


